//
//  SXLoginView.swift
//  微博09项目
//
//  Created by mac on 15/9/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
//protocol SXLoginViewDelegate:NSObjectProtocol{
//    func visitorLoginViewWillRegister();
//    func visitorLoginViewWillLogin();
//}



class SXLoginView: UIView {
    //代理方法一定要用weak开头，不然会造成死循环
   //weak var delegate: SXLoginViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame : frame)
        setAddsubview()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setAddsubview()
    }
    //监听按钮点击的方法
//    func loginButtonClick(){
//        delegate?.visitorLoginViewWillLogin()
//    }
//    func registerButtonClick(){
//        delegate?.visitorLoginViewWillRegister()
//    }
    func setupInfo(imageName:String?,message:String){
        messageLabel.text = message
        if let imgName = imageName {
            //1,替换图片
            print(imageName)
            iconView.image = UIImage(named:imgName)
            //将HomeIconView的图片设置为隐藏
            homeIconView.hidden = true
            //将遮盖图片移到控件后面
            
            sendSubviewToBack(coverImage)
        }else{
            startAnimation()
        }
    }
    
    //设置首页的动画
       func  startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.duration = 3
        anim.toValue = M_PI * 2
        
        /// 这里一定要设置动画不被删除，因为动画会在程序一运行结束就会被删除，所以这里一定设置，否则画面再次跳转的时候就没有画面
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
     func setAddsubview(){
        self.frame = UIScreen.mainScreen().bounds
       // backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1.0)
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        addSubview(iconView)
    
        addSubview(coverImage)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60))
       
        //设置homeIconView的布局
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
       
        //设置文本的自动布局
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
         addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        //设置注册按钮的自动布局
       registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //设置登陆按钮的自动布局
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item:  loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item:  loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item:  loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item:  loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        /*
        H: 水平方向
        V: 垂直方向
        | 边界
        [] 控件
        */
        //对遮盖设置自动布局
        coverImage.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":coverImage]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-(-35)-[registerButton]", options: [], metrics: nil, views: ["v":coverImage,"registerButton":registerButton]))
       
    
    }
    //设置图标
    private lazy var iconView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //设置小房子
    private lazy var homeIconView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //设置文本
    private lazy var messageLabel:UILabel = {
        let labelTitle = UILabel()
        
        labelTitle.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        labelTitle.font = UIFont.systemFontOfSize(14)
        labelTitle.numberOfLines = 0
        labelTitle.textColor = UIColor.lightGrayColor()
        
        return labelTitle
    }()
//添加注册按钮
      lazy var registerButton :UIButton = {
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
       // btn.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    lazy var loginButton: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState:UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
       // btn.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        }()
    private lazy var coverImage :UIImageView = {
        let imgView = UIImageView()
        
        imgView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return imgView
    }()
}
